<?php
namespace App\Controller;
use Think\Controller;
use App\Controller\BaseController;
class CrontabController extends BaseController {

	//清除60分钟未支付商品订单(定时任务，5分钟执行一次)
	public function clearGoodsOrder(){
		$data['order_paystatus'] = 1;
		$data['order_createtime'] = array('LT',time() - 60*60);
		M('order')->where($data)->delete();
	}

	//清除30分钟未支付考试订单，线上考试则释放考官(定时任务，5分钟执行一次)
	public function clearExamOrder(){
		$data['exam_paystatus'] = 1;
		$data['exam_status'] = 0;
		$data['exam_midiclub_id'] = -1;
		$data['exam_applytime'] = array('LT',time() - 60*30);
		$online = M('exam')->where($data)->select();
		if (!empty($online)) {
			foreach ($online as $k => $v) {
				$examiner['examoptions_user_id'] = $v['exam_examiner_id'];
				$examiner['examoptions_ymd'] = $v['exam_ymd'];
				if ($options_arr = M('examoptions')->where($examiner)->find()) {
					$old = explode(',', $options_arr['examoptions_ordertime']);
					$id = array($v['exam_time']);
					$new = array_diff($old, $id);
					$options_arr['examoptions_ordertime'] = implode(',', $new);
					M('examoptions')->save($options_arr);
				}
			}
		}
        //释放考官之后，删除无效考试订单
        unset($data['exam_midiclub_id']);
        M('exam')->where($data)->delete();
        //更新线下考试已报名人数
        $condition['examoffline_ymd'] = array('GT',date('Y-m-d',time()-24*60*60));
        $off_arr = M('examoffline')->where($condition)->select();
        if (!empty($off_arr)) {
            foreach ($off_arr as $k => $v) {
                $c['exam_ymd'] = $v['examoffline_ymd'];
                $c['exam_midiclub_id'] = $v['examoffline_roomid'];
                $c['exam_major_id'] = 1;
                $v['examoffline_already_guitar'] = M('exam')->where($c)->count();
                $c['exam_major_id'] = 2;
                $v['examoffline_already_drum'] = M('exam')->where($c)->count();
                $c['exam_major_id'] = 3;
                $v['examoffline_already_bass'] = M('exam')->where($c)->count();
                M('examoffline')->save($v);
            }
        }
	}

    //考试时间过期(每天凌晨更改前一天的线上的考试结果状态=4)
    public function examtime_expire(){
        $data['exam_ymd'] = array('LT',date('Y-m-d'));
        $data['exam_midiclub_id'] = -1;
        $data['exam_paystatus'] = 2;
        $data['exam_status'] = 1;
        $ids_arr = M('exam')->field('exam_id')->where($data)->select();
        // $ids_arr = array(array('exam_id'=>1),array('exam_id'=>5),array('exam_id'=>7),array('exam_id'=>3));
        $ids_str = '';
        if (!empty($ids_arr)) {
            foreach ($ids_arr as $k => $v) {
                if (empty($ids_str)) {
                    $ids_str = $v['exam_id'];
                }else{
                    $ids_str .= ','.$v['exam_id'];
                }
            }
            $sql = "update midi_exam set exam_result = 4,exam_status = 3 where exam_id in ({$ids_str})";
            M()->execute($sql);
        }
    }

    //线上考试前三十分钟通知
    public function notify_halfhour(){
        //当前时间戳
        $now = time();
        //时间段
        $hi['examtime_hi'] = date('H:i',$now + 30*60);
        $examtime = M('examtime')->where($hi)->find();
        if (empty($examtime)) {
            echo "无此时间段";die;
        }
        $condition['exam_ymd'] = date('Y-m-d',$now);    //年月日
        $condition['exam_time'] = $examtime['examtime_id'];    //时间段
        $condition['exam_midiclub_id'] = -1;    //线上考试
        $condition['exam_paystatus'] = 2;   //已付款
        $condition['exam_status'] = 1;   //报名成功
        $exam_arr = M('exam')->field('exam_user_id,exam_examiner_id')->where($condition)->select(); //半小时后的线上考试
        if (empty($exam_arr)) {
            echo "半小时后无线上考试";die;
        }
        $user_ids = '';
        $user_str = '';
        $examiner_ids = '';
        $examiner_str = '';
        foreach ($exam_arr as $v) {
            if(!empty($v['exam_user_id'])){
                if($user_str == ''){
                    $user_ids = $v['exam_user_id'];
                    $user_str = 'user'.$v['exam_user_id'];
                }else{
                    $user_ids .= ','.$v['exam_user_id'];
                    $user_str .= ',user'.$v['exam_user_id'];
                }
            }
            if(!empty($v['exam_examiner_id'])){
                if($examiner_str == ''){
                    $examiner_ids = $v['exam_examiner_id'];
                    $examiner_str = 'examiner'.$v['exam_examiner_id'];
                }else{
                    $examiner_ids .= ','.$v['exam_examiner_id'];
                    $examiner_str .= ',examiner'.$v['exam_examiner_id'];
                }
            }
        }
        //message
        if(!empty($user_ids)){
            $user_a = explode(',', $user_ids);
            $user_a = array_unique($user_a);
            $user_ids = implode(',', $user_a);
            $msgwhere['message_user_id'] = array('IN',$user_ids);
            $msgwhere['message_client'] = 1;
            $msgwhere['message_time'] = time();
            $msgwhere['message_type'] = 4;
            $msgwhere['message_title'] = '迷笛考级提醒您：您在'.$condition['exam_ymd'].$hi['examtime_hi'].'有考试，请准时参加！';
            $msgwhere['message_content'] = '迷笛考级提醒您：您在'.$condition['exam_ymd'].$hi['examtime_hi'].'有考试，请准时参加！';
            M('message')->data($msgwhere)->add();
        }
        if(!empty($examiner_ids)){
            $examiner_a = explode(',', $examiner_ids);
            $examiner_a = array_unique($examiner_a);
            $examiner_ids = implode(',', $examiner_a);
            $msgwhere['message_user_id'] = array('IN',$examiner_ids);
            $msgwhere['message_client'] = 2;
            $msgwhere['message_time'] = time();
            $msgwhere['message_type'] = 4;
            $msgwhere['message_title'] = '迷笛考级提醒您：您在'.$condition['exam_ymd'].$hi['examtime_hi'].'有监考，请准时监考！';
            $msgwhere['message_content'] = '迷笛考级提醒您：您在'.$condition['exam_ymd'].$hi['examtime_hi'].'有监考，请准时监考！';
            M('message')->data($msgwhere)->add();
        }
        //push
        if(!empty($user_str)){
            $user_arr = explode(',', $user_str);
            $user_arr = array_unique($user_arr);
            $user_value = array('tag'=>$user_arr);
            push($user_value,'迷笛考级提醒您：您在'.$condition['exam_ymd'].' '.$hi['examtime_hi'].'有考试，请准时参加！',C(JPUSH_USER),4);
        }
        if(!empty($examiner_str)){
            $examiner_arr = explode(',', $examiner_str);
            $examiner_arr = array_unique($examiner_arr);
            $examiner_value = array('tag'=>$examiner_arr);
            push($examiner_value,'迷笛考级提醒您：您在'.$condition['exam_ymd'].' '.$hi['examtime_hi'].'有监考，请准时监考！',C(JPUSH_EXAMINER),4);
        }
    }

    //提前一天通知
    public function notify_oneday(){
        //当前时间戳
        $now = time();
        $condition['exam_ymd'] = date('Y-m-d',$now + 24*60*60);    //年月日
        $condition['exam_paystatus'] = 2;   //已付款
        $condition['exam_status'] = 1;   //报名成功
        $exam_arr = M('exam')->alias('e')->join('left join midi_user as u on e.exam_user_id = u.user_id')->field('e.exam_user_id,e.exam_examiner_id,e.exam_midiclub_id,u.user_phone')->where($condition)->select();
        if (empty($exam_arr)) {
            echo "后面一天无考试";die;
        }
        $user_ids = '';
        $user_str = '';
        $examiner_ids = '';
        $examiner_str = '';
        $phones_online = '';
        $phones_offline = '';
        foreach ($exam_arr as $v) {
            //判断是线上还是线下
            if ($v['exam_midiclub_id'] == -1) {
                if($phones_online == ''){
                    $phones_online = $v['user_phone'];
                }else{
                    $phones_online .= ','.$v['user_phone'];
                }
            }else{
                if($phones_offline == ''){
                    $phones_offline = $v['user_phone'];
                }else{
                    $phones_offline .= ','.$v['user_phone'];
                }
            }
            if(!empty($v['exam_user_id'])){
                if($user_str == ''){
                    $user_ids = $v['exam_user_id'];
                    $user_str = 'user'.$v['exam_user_id'];
                }else{
                    $user_ids .= ','.$v['exam_user_id'];
                    $user_str .= ',user'.$v['exam_user_id'];
                }
            }
            if(!empty($v['exam_examiner_id'])){
                if($examiner_str == ''){
                    $examiner_ids = $v['exam_examiner_id'];
                    $examiner_str = 'examiner'.$v['exam_examiner_id'];
                }else{
                    $examiner_ids .= ','.$v['exam_examiner_id'];
                    $examiner_str .= ',examiner'.$v['exam_examiner_id'];
                }
            }
        }
        //message
        if(!empty($user_ids)){
            $user_a = explode(',', $user_ids);
            $user_a = array_unique($user_a);
            $user_ids = implode(',', $user_a);
            $msgwhere['message_user_id'] = array('IN',$user_ids);
            $msgwhere['message_client'] = 1;
            $msgwhere['message_time'] = time();
            $msgwhere['message_type'] = 4;
            $msgwhere['message_title'] = '迷笛考级提醒您：您在'.$condition['exam_ymd'].'有考试，请准时参加！';
            $msgwhere['message_content'] = '迷笛考级提醒您：您在'.$condition['exam_ymd'].'有考试，请准时参加！';
            M('message')->data($msgwhere)->add();
        }
        if(!empty($examiner_ids)){
            $examiner_a = explode(',', $examiner_ids);
            $examiner_a = array_unique($examiner_a);
            $examiner_ids = implode(',', $examiner_a);
            $msgwhere['message_user_id'] = array('IN',$examiner_ids);
            $msgwhere['message_client'] = 2;
            $msgwhere['message_time'] = time();
            $msgwhere['message_type'] = 4;
            $msgwhere['message_title'] = '迷笛考级提醒您：您在'.$condition['exam_ymd'].'有监考，请准时监考！';
            $msgwhere['message_content'] = '迷笛考级提醒您：您在'.$condition['exam_ymd'].'有监考，请准时监考！';
            M('message')->data($msgwhere)->add();
        }
        //push
        if(!empty($user_str)){
            $user_arr = explode(',', $user_str);
            $user_arr = array_unique($user_arr);
            $user_value = array('tag'=>$user_arr);
            push($user_value,'迷笛考级提醒您：您在'.$condition['exam_ymd'].'有考试，请准时参加！',C(JPUSH_USER),4);
        }
        if(!empty($examiner_str)){
            $examiner_arr = explode(',', $examiner_str);
            $examiner_arr = array_unique($examiner_arr);
            $examiner_value = array('tag'=>$examiner_arr);
            push($examiner_value,'迷笛考级提醒您：您在'.$condition['exam_ymd'].'有监考，请准时监考！',C(JPUSH_EXAMINER),4);
        }
        //短信发送（阿里大鱼调用一次接口最多发送200条短信）
        //线上
        if(!empty($phones_online)){
            $phones_online_arr = explode(',',$phones_online);
            $new_phones_online = array_chunk($phones_online_arr,200);
            foreach ($new_phones_online as $k => $v) {
                $phones_online_str = implode(',',$v);
                alidayumsg($phones_online_str,'','SMS_80920019');
            }
        }
        //线下
        if(!empty($phones_offline)){
            $phones_offline_arr = explode(',',$phones_offline);
            $new_phones_offline = array_chunk($phones_offline_arr,200);
            foreach ($new_phones_offline as $k => $v) {
                $phones_offline_str = implode(',',$v);
                alidayumsg($phones_offline_str,'','SMS_80860022');
            }
        }

    }

    //清理usersig过期 (定时任务，每天3点执行一次)
    public function usersigExpire(){
        $sql = "update midi_user set user_sig = '' where user_sigexpire < ".time();
        M()->execute($sql);
    }



}